#!/bin/bash
OK=0
WARN=1
CRITICAL=2
UNKNOWN=3
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
EPATH="/usr/lib64/nagios/plugins"


Ok_name=''
Fail_name=''
Judge=0
gfsmount=''

host=`hostname`
mylog="/var/log/messages"
currentcpuload=`uptime | awk -F"load average:" '{print $NF}'|awk -F" " '{print $1}'|awk -F"." '{print $1}'`
if [ $currentcpuload -ge 100 ];then
    echo "cpuload is $currentcpuload"
    exit $CRITICAL
fi

if [ -f /etc/init.d/gluster ];then
	gfsmount="besfs2"
else
	echo "glusterfs isnot installed!"
	exit 2

fi


for choice in $gfsmount                    ###
do
        case $choice in
	      besfs2)
		TempE=`ps aux|grep -v grep|grep "/besfs2"`
                if [ "t$TempE" == "t" ];then
                        /etc/init.d/gluster stop
                        rm -rf /etc/mtab~*
                        umount -l /besfs2
                        rm -rf /etc/mtab~*
                	/etc/init.d/gluster start        
                fi

	     #check mount point12yy
		/usr/bin/timeout 15 strace test -w /besfs2/nms/testfs 1>/dev/null 2>&1
		if [ $? != 0 ]
		then
                        Judge=1
                        #/etc/init.d/gluster stop 1>/dev/null 2>&1
                        #/etc/init.d/gluster start 1>/dev/null 2>&1
                        #str1=`date|cut -d" " -f2-4`
			str1=`date "+%b %d %H:%M:%S"`
                        msg=$str1" "$host" gluster_remount: besfs2_client dropped"
                        echo $msg >> $mylog
			/usr/bin/timeout 3 strace test -w /besfs2/nms/testfs 1>/dev/null 2>&1
                	if [ $? != 0 ]
			then 
				Judge=1
				echo "df /besfs2 time-out"
			fi
                        exit 2
                fi
              
              mem_use=`ps aux|grep besvol|grep -v grep|awk 'NR==1 {print $6}'`

              if [ $mem_use -gt 3145728 ]
              then
                        echo "gluster used memory: $mem_use KB, which is over used"
			rm -rf /etc/mtab~*
                        /etc/init.d/gluster stop 1>/dev/null 2>&1
                        /etc/init.d/gluster start 1>/dev/null 2>&1
                        str1=`date|cut -d" " -f2-4`
                        msg=$str1" "$host" gluster_remount: besfs2_memory gluster memory error"
                        echo $msg >> $mylog
                        exit $CRITICAL

             fi

	     # check mount point size 
              M_PATH=/besfs2
              M_Total=`cat $M_PATH/.mount`

              if [ -e /$M_PATH/.mount ]
              then

                Local_Total=`$EPATH/check_disk -t 15 -m $M_PATH |awk -F ';' '{print $NF}'`
                #echo "Local_Total $Local_Total $M_Total"
                if (( $Local_Total < $M_Total ))
                then
                        echo "$M_PATH size $Local_Total MB smaller than $M_Total"
                        exit $CRITICAL
                elif (( $Local_Total > $M_Total ))
                then
                        echo "$M_PATH size $Local_Total MB larger than $M_Total"
                        exit $CRITICAL
                else
                        echo "$M_PATH size is OK"
                        exit $OK
                fi

             else
                exit $CRITICAL
                echo "Mount point $M_PATH is wrong"
             fi
              ;;

        esac

done

if [ $Judge -eq 1 ]
then
        echo "gluster: $Fail_name  wrong"
        exit $CRITICAL
else
        echo " gluster:$Ok_name are OK "
        exit $OK
fi
